package android.slc.appbase.ui.vd;

import android.slc.appbase.api.slc.callback.SlcObserver;
import android.slc.appbase.ui.vd.wrapper.BaseListPageByFxWrapper;
import android.slc.appbase.ui.vd.wrapper.IBaseListPageByFxWrapper;
import android.slc.box.adapter.LoadMoreDataFormatByFx;
import android.slc.box.adapter.PageModel;
import android.slc.box.commonlibrary.compat.ValueWrap;
import android.slc.box.or.SlcCallbackConfig;
import javafx.beans.property.ReadOnlyObjectProperty;

import java.io.Serializable;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public abstract class BaseListPageVmBoxByFx<T extends Serializable> extends BaseListCommonVmBox<T> implements IBaseListPageByFxWrapper<T> {
    private final Map<Integer, PageModel<T>> cachePageModel = new HashMap<>();
    protected final BaseListPageByFxWrapper<T> listPageByFxWrapper;

    public BaseListPageVmBoxByFx() {
        this.listPageByFxWrapper = createListWrapper();
    }

    protected BaseListPageByFxWrapper<T> createListWrapper() {
        assertListPageByFxWrapper();
        return new BaseListPageByFxWrapper<T>(this) {

            @Override
            protected void onLoadMore() {
                BaseListPageVmBoxByFx.this.onLoadMore();
            }
        };
    }

    protected final void assertListPageByFxWrapper() {
        if (listPageByFxWrapper != null) {
            throw new RuntimeException("listPageByFxWrapper已存在不允许重复创建！");
        }
    }

    @Override
    public ReadOnlyObjectProperty<ValueWrap<List<T>>> getDataListObserver() {
        throw new RuntimeException("不要用这个，没有意义");
    }

    @Override
    public List<T> getDataList() {
        throw new RuntimeException("不要用这个，没有意义");
    }

    /**
     * 根据分页获取数据列表
     *
     * @return
     */
    protected List<T> getDataListByPageIndex() {
        return getDataListByPageIndex(getLoadMoreDataFormatByFx().getOffset());
    }

    protected List<T> getDataListByPageIndex(int office) {
        PageModel<T> pageModel = this.cachePageModel.get(office);
        if (pageModel != null) {
            return pageModel.getListNoNull();
        }
        return null;
    }

    @Override
    public void refresh() {
        this.cachePageModel.clear();
        this.listPageByFxWrapper.refresh();
    }

    /**
     * 首次刷新
     */
    public void firstRefresh() {
        LoadMoreDataFormatByFx<T> loadMoreDataFormatByFx = this.listPageByFxWrapper.loadMoreDataFormatByFx;
        loadMoreDataFormatByFx.setOffset(loadMoreDataFormatByFx.getBaseOffset());
        refresh();
        loadMore();
    }

    protected abstract void onLoadMore();

    @Override
    public final void loadMore() {
        this.listPageByFxWrapper.loadMore();
    }

    public void loadMore(int pageIndex) {
        loadMore(pageIndex, true);
    }

    public void loadMore(int pageIndex, boolean useCache) {
        this.listPageByFxWrapper.loadMoreDataFormatByFx.setOffset(pageIndex);
        if (useCache) {
            PageModel<T> pageModel = this.cachePageModel.get(pageIndex);
            if (pageModel != null) {
                this.listPageByFxWrapper.onSucceed(this.cachePageModel.get(pageIndex));
                return;
            }
        }
        this.loadMore();
    }

    @Override
    public void onSucceed(PageModel<T> data) {
        this.cachePageModel.put(data.getCurrent(), data);
    }

    @Override
    public final void onFailed(int errorCode, String errorMessage) {
        super.onFailed(errorCode, errorMessage);
    }

    @Override
    public void onError(int currentPageIndex, int errorCode, String errorMessage) {
        onFailed(errorCode, errorMessage);
    }

    @Deprecated
    @Override
    public SlcObserver<PageModel<T>> getDataListSlcObserver() {
        return createSlcObserver();
    }

    @Override
    public LoadMoreDataFormatByFx<T> getLoadMoreDataFormatByFx() {
        return this.listPageByFxWrapper.getLoadMoreDataFormatByFx();
    }

    @Override
    public SlcObserver<PageModel<T>> createSlcObserver() {
        return this.listPageByFxWrapper.createSlcObserver();
    }

    @Override
    public SlcObserver<PageModel<T>> createSlcObserver(int pageIndex) {
        return this.listPageByFxWrapper.createSlcObserver(pageIndex);
    }

    @Override
    public SlcObserver<PageModel<T>> createSlcObserver(int pageIndex, SlcCallbackConfig slcCallbackConfig) {
        return this.listPageByFxWrapper.createSlcObserver(pageIndex, slcCallbackConfig);
    }

}
